#!/bin/bash
#=====================================================================
#
# $Id: mainMenu.sh 26 2010-11-29 11:53:05Z cvs $
# 
# FILE:    mainMenu.sh
#
# PURPOSE: configuration mainMenu for AIX-ORACLE-CONFIG
#
# AUTHOR:  M.Wirz
# Copyright (C) [2010] [Michael Wirz]
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 3 of
# the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, see <http://www.gnu.org/licenses/>.
#====================================================================
#  SVN Propset
#  $Author: cvs $
#  $Date: 2010-11-29 12:53:05 +0100 (Mon, 29 Nov 2010) $
#  $Revision: 26 $
#  $Id: mainMenu.sh 26 2010-11-29 11:53:05Z cvs $
#  $URL: file:///data/svn.gallery.li/installation/mainMenu.sh $
#====================================================================

_temp="/tmp/answer.$$"
source ./bin/functions.sh
source ./etc/BaseConfig
if [ "`uname`" == "AIX" ]
then
  OS=AIX
  source ./etc/AIXConfig
elif [ "`uname`" == "Linux" ]
then
  OS=LINUX
  source ./etc/LinuxConfig
else
   OS=UNKNOWN
   exit 1
fi

PN=`basename "$0"`
VER='0.31'
${DIALOG} 2>$_temp
DVER=`cat $_temp | head -1`


### create main menu using ${DIALOG}
main_menu() {
    ${DIALOG} --backtitle "AIX-ORACLE-CONFIG" --title " Main Menu - V. $VER "\
        --cancel-label "Quit" \
        --menu "Move using [UP] [DOWN], [Enter] to select" 17 60 10\
	InstallOraBin	"Install Oracle Binaries" \
	InstallDB		"Create DB Skripts"\
    Quit			"Exit demo program" 2>$_temp

    opt=${?}
    if [ $opt != 0 ]; then rm $_temp; exit; fi
    menuitem=`cat $_temp`
    echo "menu=$menuitem"
    case $menuitem in
        InstallOraBin) createMenu InstallOraBin functMenuLinesForConfig;;
	CreateBackupScripts) createMenu createBackupScripts functMenuLinesForConfig;;
        Quit) rm $_temp; exit;;
    esac
}















































############DIALOG FUNCTIONS STILL NEEDED!!!!!
### create main menu using ${DIALOG}
main_menuOLD2() {
    ${DIALOG} --backtitle "TVD-OSS-Config" --title " Main Menu - V. $VER "\
        --cancel-label "Quit" \
        --menu "Move using [UP] [DOWN], [Enter] to select" 17 60 10\
        NewInstall "Install on a new server"\
        ReInstall "ReInstall an existing server"\
        ModifyConfig "Modifie the config file of an existing server"\
        ConfigInstallServer "Set Basic Informations (installserver ip etc.)"\
        Version "Show program version info"\
        Quit "Exit demo program" 2>$_temp
        
    opt=${?}
    if [ $opt != 0 ]; then rm $_temp; exit; fi
    menuitem=`cat $_temp`
    echo "menu=$menuitem"
    case $menuitem in
        NewInstall) chooseWhat2Do;;
        ReInstall) chooseServer;;
        ModifyConfig) createMenu ChooseConfigFile functMenuLinesForConfig;;
        ConfigInstallServer) 
        fileNameArray=(${InstallServerConfigFields[*]})
        exampleArray=(${InstallServerConfigFieldsValues[*]})
        createForms OSConfiguration functFormLinesFromArray;;
        Version) version;;
        Quit) rm $_temp; exit;;
    esac
}

createMenu() { 
title=${1}
type=${2}
filename=${3}
    ${DIALOG} --backtitle "${title}" --title "${title}"\
        --cancel-label "Quit" \
        --menu "Move using [UP] [DOWN], [Enter] to select" 17 60 10\
	`${type}` 2>$_temp

    opt=${?}
    if [ $opt != 0 ]; then rm $_temp; exit; fi
    configFile=`cat $_temp`
    ${title}
}

chooseWhat2Do() {
    result=`${DIALOG} --stdout --backtitle "What you wanna do today" \
           --radiolist "items to choose" 15 50 8 \
           01 "Install Linux Base System" on\
           02 "Install Oracle 10.2.0.1" off\
           03 "Install Oracle 10.2.0.4" off\
           04 "Install Oracle Barebone Database" off`
        case $result in
        01) OSInstallWizard;;
        02) ora10201InstallWizard;;
        ##03) ora10204InstallWizard;;
        03) fileNameArray=(${ORAConfigFields[*]})
            exampleArray=(${ORAConfigFieldsValues[*]})
            createForms OSConfiguration functFormLinesFromArray;;
        04) oraBareboneInstallWizard;;
        Quit) rm $_temp; exit;;
    esac   
}    

OSInstallWizard() {
    result=`${DIALOG} --stdout --backtitle "What you wanna do today" \
           --radiolist "items to choose" 15 50 8 \
           01 "Create new configuration file" on\
           02 "Use existing file as template" off\
           03 "go Back to previous screen" off`
        case $result in
        01) fileNameArray=(${OSConfigFields[*]})
	    exampleArray=(${OSConfigFieldsValues[*]})
	    createForms OSConfiguration functFormLinesFromArray;;
        #02) createForms OSConfiguration functFormsLinesFromTemplate "./OSConfigs/serverA";;
        02)  chooseFile $configDir createForms OSConfiguration functFormsLinesFromTemplate;;
        03) chooseWhat2Do;;
        Quit) rm $_temp; exit;;
    esac   

}

chooseFile() {
    result=`${DIALOG} --stdout --backtitle "Dialog - fselect sample"\
           --begin 3 10 --title " use [blank] key to select "\
           --fselect "$1" 10 60` 
$2 $3 $4 $result
}

functFormLinesFromArray() {
A=0
while [ $A -lt  ${#fileNameArray[@]} ]
do
  echo ''${fileNameArray[$(($A))]}' '$((A+1))' 4 '${exampleArray[$(($A))]}' '$((A+1))' 40 200 0\'
  A=$((A+1))
done
}

functFormsLinesFromTemplate() {
exampleArray=()
fileNameArray=()
if [ "${toGrep}" == "" ]
then
  toGrep="="
fi
N=0
grep ${toGrep} ${filename} | while read LINE
  do N=$((N+1))
      exampleArray[$(($N-1))]=${LINE##*=}
      fileNameArray[$(($N-1))]=${LINE%%=*}
      echo ${LINE%%=*} $((N+1)) 4 ${LINE##*=} $((N+1)) 40 250 0 
  done 
A=0
}

functMenuLinesForConfig() {
for i in `ls ${configDir}`
do
  ORACLE_TARGET_VERSION=`grep "ORACLE_TARGET_VERSION=" ${configDir}/${i}`
  ORACLE_TARGET_VERSION=${ORACLE_TARGET_VERSION##*=}
  echo ''$i' '${ORACLE_TARGET_VERSION}''
done
}


createForms() {
title=${1}
type=${2}
filename=${3}
N=0
  ${DIALOG} --stdout --backtitle "${title}"\
  --form " Form Test - use [up] [down] to select input field" 21 250 18 \
  `${type}`
}

oldcreateForms() {
title=${1}
type=${2}
filename=${3}
N=0
  ${DIALOG} --stdout --backtitle "${title}"\
  --form " Form Test - use [up] [down] to select input field" 21 250 18 \
  `${type}` | while read LINE
  do N=$((N+1))
    if [ -z $LINE ]
    then
      ${DIALOG} --msgbox "Value ${fileNameArray[$(($N-1))]} is empty. Please correct" 6 42
      echo "1" > /tmp/newStart	
    else
      exampleArray[$(($N-1))]=$LINE
    fi	
  done
  if [ "`cat /tmp/newStart`" == "1" ]
  then
    exampleArray=( `cat /tmp/exampleArray` )
    createForms ${title} ${type}
  else
    rm /tmp/newStart /tmp/exampleArray
    wrote2File
  fi
exit 0
}	

main_menuold() {
    ${DIALOG} --backtitle "TVD-OSS-Config" --title " Main Menu - V. $VER "\
        --cancel-label "Quit" \
        --menu "Move using [UP] [DOWN], [Enter] to select" 17 60 10\
        OS-Config "Operating System Configuration"\
        ORA-Config "Oracle Basic Configuration"\
        DG-Config "Oracle Dataguard Configuration"\
        Version "Show program version info"\
        Quit "Exit demo program" 2>$_temp
        
    opt=${?}
    if [ $opt != 0 ]; then rm $_temp; exit; fi
    menuitem=`cat $_temp`
    echo "menu=$menuitem"
    case $menuitem in
        OS-Config) subOSConfig;;
        ORA-Config) subOraConfig;;
        DG-Config) subdDGConfig;;
        Version) version;;
        Quit) rm $_temp; exit;;
    esac
}

subOSConfig() {
    ${DIALOG} --backtitle "TVD-OSS-Config" --title " Main Menu - V. $VER "\
        --cancel-label "Quit" \
        --menu "Move using [UP] [DOWN], [Enter] to select" 17 60 10\
        IP-Config "Setup the network"\
        Quit "Exit demo program" 2>$_temp
    opt=${?}
    if [ $opt != 0 ]; then rm $_temp; exit; fi
    menuitem=`cat $_temp`
    echo "menu=$menuitem"
    case $menuitem in
        IP-Config) subIPConfig;;
        Quit) rm $_temp; exit;;
    esac
}

subIPConfig() {
    ${DIALOG} --backtitle "TVD-OSS-Config" --title " Main Menu - V. $VER "\
        --cancel-label "Quit" \
        --menu "Move using [UP] [DOWN], [Enter] to select" 17 60 10\
        IP-Config "Setup the network"\
        Quit "Exit demo program" 2>$_temp
    opt=${?}
    if [ $opt != 0 ]; then rm $_temp; exit; fi
    menuitem=`cat $_temp`
    echo "menu=$menuitem"
    case $menuitem in
        IP-Config) subIPConfig;;
        Quit) rm $_temp; exit;;
    esac
}

subOSConfig2() {
configFile=./OSConfigs/newServer
newArray=""
for i in `cat ${configFile}`
do
  newArray=`echo ${newArray} ${i%%=*} ${i##*=}`
done
outputtoArray=( `echo ${newArray}` )
    editParameter=`${DIALOG} --stdout --backtitle "Dialog - Sample menu with variable items" \
           --title "Select a file" --menu \
           "Choose one of the menu points" 16 40 8 ${outputtoArray[*]}`
    para1=`cat ${configFile}|grep ${editParameter}`
    if [ $? -eq 0 ]; then
        value=`${DIALOG} --stdout --inputbox ${para1%%=*} 6 42 ${para1##*=}`
        sed -i 's/'${para1}'/'${para1%%=*}'='${value}'/g' ${configFile}
    fi
}

### File or Directory selection menu with ${DIALOG}
file_menu() {
    fileroot=$HOME
    IFS_BAK=$IFS
    IFS=$'\n' # wegen Filenamen mit Blanks
    array=( $(ls $fileroot) )
    n=0
    for item in ${array[@]}
    do
        menuitems="$menuitems $n ${item// /_}" # subst. Blanks with "_"  
        let n+=1
    done
    IFS=$IFS_BAK
    ${DIALOG} --backtitle "Dialog - Sample menu with variable items" \
           --title "Select a file" --menu \
           "Choose one of the menu points" 16 40 8 $menuitems 2> $_temp
    if [ $? -eq 0 ]; then
        item=`cat $_temp`
        selection=${array[$(cat $_temp)]}
        ${DIALOG} --msgbox "You choose:\nNo. $item --> $selection" 6 42
    fi
}

### File Select sample 
file_select() {
    ${DIALOG} --backtitle "Dialog - fselect sample"\
           --begin 3 10 --title " use [blank] key to select "\
           --fselect "$HOME/" 10 60 2>$_temp

    result=`cat $_temp`
    ${DIALOG} --msgbox "\nYou selected:\n$result" 9 52
}

### create Today's calendar ###
calendar() {
    today=date +"%d %m %Y"
    echo "heute=$today"
    ${DIALOG} --backtitle "Dialog - Calendar sample" \
           --calendar "choose a date" 2 1 $today 2>$_temp
    datum=`cat $_temp`
    ${DIALOG} --title " Date selected " --msgbox "\nYour date: $datum" 6 30
}    

### Check List - multi select sample ###
checklist() {
    ${DIALOG} --backtitle "Dialog - CheckList (multi select) sample" \
           --checklist "tag item(s) to choose" 15 50 8 \
           01 "first item to select" off\
           02 "second item - on by default" on\
           03 "third item" off\
           04 "more items ..." off 2>$_temp
    result=`cat $_temp`
    ${DIALOG} --title " Item(s) selected " --msgbox "\nYou choose item(s): $result" 6 44
}    

### Radio List - single select sample ###
radiolist() {
    ${DIALOG} --backtitle "Dialog - RadioList (single select) sample" \
           --radiolist "tag item to choose" 15 50 8 \
           01 "first item to select" off\
           02 "second item - on by default" on\
           03 "third item" off\
           04 "more items ..." off 2>$_temp
    result=`cat $_temp`
    ${DIALOG} --title " Item(s) selected " --msgbox "\nYou choose item: $result" 6 44
}    

### Input Box sample 
inputbox() {
    ${DIALOG} --backtitle "Dialog - InputBox sample"\
           --inputbox "Enter a line, please" 8 52 2>$_temp

    result=`cat $_temp`
    ${DIALOG} --msgbox "\nYou entered:\n$result" 9 52
}

### Message Box sample - show versions 
version() {
    ${DIALOG} --backtitle "Dialog - MessageBox sample" \
           --msgbox "$PN - Version $VER\na Linux ${DIALOG} Tutorial\n\nusing:\n$DVER" 9 52
}

### Text Box sample - show file test.txt
textbox() {
    filename="test.txt"
    if [ -e $filename ]; then
        ${DIALOG} --backtitle "Dialog - TextBox sample - use [up] [down] to scroll"\
               --begin 3 5 --title " viewing File: $filename "\
               --textbox $filename 20 70
    else
        ${DIALOG} --msgbox "*** ERROR ***\n$filename does not exist" 6 42
    fi
}

### Form Sample ###
formbox () {
	${DIALOG} --backtitle "Dialog - Form sample" \
	    --form " Form Test - use [up] [down] to select input field " 21 70 18 \
	    "name" 2 4 "" 2 15 20 0\
	    "surname" 4 4 "" 4 15 20 0\
	    "city" 6 4 "" 6 15 20 0\
	    "county" 8 4 "Germany" 8 15 "20" 0\
	    2>$_temp
	
	if [ ${?} -ne 0 ]; then return; fi   
    result=`cat $_temp`
    echo "Result=$result"
    ${DIALOG} --title "Items are separated by \\n" --cr-wrap \
           --msgbox "\nYou entered:\n$result" 12 52
}


### Text Box sample - show file test.txt
tailbox() {
    ${DIALOG} --backtitle "Dialog - TailBox sample"\
           --begin 3 5 --title " viewing File: /var/log/messages "\
           --tailbox /var/log/messages 18 70
}



### Creating the menu
while true; do
  main_menu
done
